import {
  Dependencies,
  MiddlewareConsumer,
  Module,
  RequestMethod,
} from "@nestjs/common";
import { APP_GUARD } from "@nestjs/core";
import { TypeOrmModule } from "@nestjs/typeorm";
import { DataSource } from "typeorm";
import { AppController } from "@/app.controller";
import { AppService } from "@/app.service";
import { defaultOpts } from "@/config/orm.config";
import { JwtAuthGuard } from "@/modules/auth/guards/jwt-auth.guard";
import { DownloadModule } from "@/modules/download/download.module";
import { UploadModule } from "@/modules/upload/upload.module";
import { AuthModule } from "@/modules/auth/auth.module";
import { UserModule } from "@/modules/system/user/user.module";
import { TenantModule } from "@/modules/tenant/tenant.module";
import { MenuModule } from "@/modules/system/menu/menu.module";
import { RoleModule } from "@/modules/system/role/role.module";
import { DeptModule } from "@/modules/system/dept/dept.module";
import { LogModule } from "@/modules/system/log/log.module";
import { DictionaryTypeModule } from "@/modules/system/dictionaryType/dictionary-type.module";
import { DictionaryModule } from "@/modules/system/dictionary/dictionary.module";
import { ApiModule } from "@/modules/system/api/api.module";
import { RolesGuard } from "@/modules/auth/guards/roles.guard";
import { TenantMiddleware } from "./common/middleware/tenant.middleware";

@Dependencies(DataSource)
@Module({
  imports: [
    DownloadModule,
    UploadModule,
    AuthModule,
    RoleModule,
    UserModule,
    TenantModule,
    MenuModule,
    DeptModule,
    LogModule,
    DictionaryTypeModule,
    DictionaryModule,
    ApiModule,
    TypeOrmModule.forRoot(defaultOpts),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(TenantMiddleware)
      .forRoutes({ path: "*", method: RequestMethod.ALL });
  }
}
